'use strict';

angular.module('myApp.students_classmanage', [])

.controller('students_classmanageCtrl',['$scope','$routeParams','$http', 
	
    function($scope,$routeParams,$http){

    $scope.stuid = $routeParams.id;
    $http.get('v1/student/'+ $routeParams.id + '/classes').success(function(data){
        $scope.students = data.result['data'];
        console.log($scope.students);
        if (data.result['data'].length == 0) {
            $('#open').show();
            $('#change').hide();
        }else{
            $('#change').show();
            $('#open').hide();
        };
    });

    //班级
    $http.get('v1/classes').success(function(data){
        $scope.model = data.result['data'];
          console.log($scope.model);
    });

    //升班
    $scope.open = function(m){
        var data = {'classes_id':m};
        $http.post('v1/student/' +$routeParams.id + '/go_up',data).success(function(data){
            if( !data.error ){
                alert("升班成功","success");
                $http.get('v1/student/'+ $routeParams.id + '/classes').success(function(data){
                    $scope.students = data.result['data'];
                    if (data.result['data'].length == 0) {
                        $('#open').show();
                        $('#change').hide();
                    }else{
                        $('#change').show();
                        $('#open').hide();
                    };         
                });
            }else{
                alert(data.message);
            }
        });
    };

    //退学 
    $scope.out = function(){
        $http.get('v1/student/'+$routeParams.id+'/quit').success(function(data){        
            console.log(data);
            alert("退学成功","success");
            $http.get('v1/student/'+ $routeParams.id + '/classes').success(function(data){
                $scope.students = data.result['data'];
                $('#open').show();
                $('#change').hide();
            });   
         });         
    };

    $scope.delete = function(student){
        console.log(student.enter_time);
        if (student.leave_time == null) {
            alert("当前所在班级不可删除");
        }else{
             console.log(student.leave_time); 
            $http.delete('v1/student/'+student.student_id+ '/classes/'+student.id).success(function(data){
                if( !data.error ){
                    alert("删除成功","success");
                    $http.get('v1/student/'+ $routeParams.id + '/classes').success(function(data){
                        $scope.students = data.result['data'];
                        console.log($scope.students);
                        if (data.result['data'].length == 0) {
                            $('#open').show();
                            $('#change').hide();
                        }else{
                            $('#change').show();
                            $('#open').hide();
                        };
                    });
                }else{
                    alert(data.message);
                }
            });        
        };
    };
}]);
